fix: encoder out of bounds#59
Merged
viviveevee merged 4 commits intomainfrom Apr 23, 2026
Merged
Conversation
viviveevee
commented
Apr 23, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes an out-of-bounds write in the CBOR encoder that could throw RangeError: Offset is outside the bounds of the DataView when a map value lands exactly on the initial 2 KB buffer boundary. It centralizes buffer growth in the common header write path and adds regression coverage for the boundary case.
Changes:
- Add a
growBuffer(minSize)helper and move buffer-capacity checks intoencodeHeader(plus reuse the helper inencodeBytes). - Add regression tests that reproduce the 2 KB boundary overflow for
encodeandencodeWithSelfDescribedTag, plus a realistic envelope-shaped payload case. - Update test TS config to use
module: ESNext(and reformatinclude).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tsconfig.test.json | Sets module: ESNext for the test TS config and reformats includes. |
| src/encode/encode.ts | Fixes encoder buffer growth by ensuring bounds checks happen on the shared header write path and by introducing growBuffer. |
| src/encode/encode.spec.ts | Adds regression tests validating the encoder no longer overflows at the 2 KB boundary. |
| CHANGELOG.md | Updates the Unreleased Fix entry to mention the encoder overflow fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: encoder buffer overflow when map value fills initial buffer exactly
Reported here
Summary
RangeError: Offset is outside the bounds of the DataViewthrown when a map value's encoded bytes land exactly on the 2 KB internal buffer boundary.encodeItemintoencodeHeader, which is the common write path for all encoding flows. The previous check inencodeItemwas bypassed whenencodeMapcalledencodeTextString(key)directly.growBufferhelper that doubles the buffer until it meets the required minimum size.Root cause
encodeMapencodes keys viaencodeTextString(key)without going throughencodeItem, so the bounds check never ran. When a preceding value (e.g. a largeUint8Array) filled the buffer to its exact capacity, the next key'sencodeHeadercall wrote past the end of theDataView.Test plan
encode.spec.tscovering encode,encodeWithSelfDescribedTag, and a realistic ICP envelope payload